/**
 * Created by zheng on 2018/12/16.
 */
import React from 'react'
import io from 'socket.io-client'

class WebSocketDemo extends React.Component {
  constructor() {
    super()
  }

  componentDidMount(){
    this.socket = io('http://localhost:10202')
    let sock = this.socket;
    sock.on('connect', ()=>{
      sock.on('hi', this.onReceivedHi);
    });
  }

  componentWillDestroy() {
    this.socket.ondisconnect();
  }

  state = {
    socket: null,
    test: ''
  }

  onReceivedHi = (data) => {
    console.log(data);
    this.setState({test: data.hello});
  }

  render() {
    return (
      <div>{this.state.test}</div>
    )
  }
}

export default WebSocketDemo
